/**
 * 
 */
package com.randis.dailyplanner.controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import com.randis.dailyplanner.ModelAndView;
import com.randis.dailyplanner.ServiceLocator;
import com.randis.dailyplanner.domain.Activity;
import com.randis.dailyplanner.domain.User;
import com.randis.dailyplanner.managers.UserManager;

/**
 * @author 1
 *
 */
public class UserController {

	private UserManager manager = ServiceLocator.getInstance().getUserManager();

	public ModelAndView editProfile(HttpServletRequest request, HttpServletResponse response) {
    	
    	User user = manager.getCurrentUser();
    	
    	Map<String, Object> model = new HashMap<String, Object>();
        model.put("user", user);
        return new ModelAndView(model, "/user_profile.jsp");
    }

    public ModelAndView saveProfile(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
    	User user = manager.getCurrentUser();

    	BeanWrapper wrapper = new BeanWrapperImpl(user);
    	Map<String, Object> parameterMap = new HashMap<String, Object>(request.getParameterMap());
    	parameterMap.remove("id");
    	wrapper.setPropertyValues(parameterMap);
    	manager.saveUser((User) wrapper.getWrappedInstance());
    	
    	response.sendRedirect("/controllers/activities/list");
    	
        return null;
    }
}
